package com.cy.controller;



import com.cy.bfzcommons.pojo.Role;
import com.cy.bfzcommons.service.RoleService;
import com.cy.bfzcommons.web.util.JsonResult;
import com.cy.bfzcommons.web.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 不传参就查询所有    传参根据名字模糊查询
     * @param roleName
     * @return
     */

    @GetMapping()
    public JsonResult findRole(@ApiParam(name="roleName",value="角色名",required=true)String roleName){
        return JsonResult.ok( PageUtil.startpage().doSelectPageInfo(()->
                roleService.findRole(roleName)));
    }

    /**
     * 根据id查询角色信息
     * @param roleId
     * @retur
     */

    @GetMapping("selectById/{roleId}")
    public JsonResult selectById(@ApiParam(name="roleId",value="角色id",required=true)@PathVariable Integer roleId){
        Role role = roleService.selectById(roleId);
        return JsonResult.ok(role);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */

    @PostMapping
    public JsonResult insertRole(@ApiParam(name="role",value="角色信息",required=true)@RequestBody Role role){
        roleService.insertRole(role);
        return JsonResult.ok();
    }



    /**
     * 根据id修改角色信息   也可用于修改状态
     */

    @PutMapping
    public JsonResult updateRole(@ApiParam(name="role",value="角色信息",required=true)@RequestBody Role role){
        System.out.println(role.getRightsList());
        roleService.updateRole(role);
        return JsonResult.ok();
    }


    /**
     * 查询所有角色的id和名字
     */

    @GetMapping("selectName")
    public JsonResult selectRoleName(){
        List<Role> list = roleService.selectRoleName();
        return JsonResult.ok(list);
    }


    /**
     * 删除该角色
     *
     * 问题1:  删除该角色的相关权限
     * 问题2:  删除拥有该角色的的管理员的关联(删除中间表中该角色的信息)
     */

    @DeleteMapping("/{roleId}")
    @Transactional
    public JsonResult deleteById(@ApiParam(name="roleId",value="角色id",required=true)@PathVariable Integer roleId){
        roleService.deleteById(roleId);
        return JsonResult.ok();
    }
}
